package com.icehand.echarts.web.util;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author zhoujunmei
 * @date 2019-08-05
 */
@Data
@Accessors(chain = true)
public class ImageResult {
    private String path;
    private byte[] bytes;

    public byte[] getImageData() throws IOException {
        if(null == path || "".equalsIgnoreCase(path)){
            return null;
        }

        if(null != bytes){
            return bytes;
        }

        File file = ResourceUtils.getFile(path);
        if(null == file){
            return null;
        }

        FileInputStream fs = new FileInputStream(file);
        bytes = new byte[fs.available()];
        fs.read(bytes);
        fs.close();
        return bytes;
    }
}
